package cn.com.utt.common.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

/**
 * @author  chen.bin
 * @date    2014-02-25
 */
public class RPCException extends Exception{
	private static final long serialVersionUID = 1L;
	private Throwable cause;
	private static Logger logger = Logger.getLogger(RPCException.class);
	
	public RPCException(String msg){
		super(msg);
		logger.error(msg);
	}
	
	public RPCException(Throwable e){
		super(e.getMessage());
		this.cause=e;
		logger.error(e.getMessage(), e);
	}
	
	public RPCException(String msg,Throwable e){
		super(msg);
		this.cause=e;
		logger.error(msg, e);
	}
	
	public Throwable getCause(){
		return this.cause;
	}
	
	public void printStackTrace(PrintStream ps){
		if(getCause()==null){
			super.printStackTrace(ps);
		}else {
			ps.println(this);
			getCause().printStackTrace(ps);
		}
	}
	
	public void printStackTrace(PrintWriter pw){
		if(getCause()==null){
			super.printStackTrace(pw);
		}else{
			pw.println(this);
			getCause().printStackTrace(pw);
		}
	}
	
	public void printStackTrace(){
		printStackTrace(System.err);
	}
}
